﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric;
using YArchitech.Revit.RevitExtension;
using YJKPresentation.CommonUnit;
using YJKPresentation.Utils;

namespace YJKPresentation.CrossDocument
{
	public class WireCopier
	{
		public WireCopier(ExternalCommandData cmdData, Autodesk.Revit.DB.Document sourceDoc, Transform transform, bool mirrored, double tolerance)
		{
			this.m_Revit = cmdData;
			this.m_CurrentDocument = this.m_Revit.Application.ActiveUIDocument.Document;
			this.m_SourceDocument = sourceDoc;
			this.m_Transform = transform;
			this.m_IsMirrored = mirrored;
			this.m_Tolerance = tolerance;
		}

		public void CopyWire(Element parEnt)
		{
			Wire wire = parEnt as Wire;
			FindCurveInfo newPosition = Locator.GetNewPosition((wire.Location as LocationCurve).Curve, this.m_Transform, this.m_IsMirrored);
			if (newPosition == null)
			{
				return;
			}
			Curve curve = this.CreateCurveByCurveInfo(newPosition);
			ElementId typeId = wire.GetTypeId();
			WireType wireType = this.m_SourceDocument.GetElement(typeId) as WireType;
			if (wireType == null)
			{
				return;
			}
			WireType wireType2 = this.FindWireType(this.m_CurrentDocument, wireType.Name);
			if (wireType2 == null)
			{
				return;
			}
			Transaction transaction = new Transaction(this.m_CurrentDocument);
			transaction.Start("new wire");
			Wire wire2 = null;
			try
			{
				List<XYZ> list = new List<XYZ>();
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				if (curve is Arc)
				{
					Arc arc = curve as Arc;
					list.Add(hyendPoint);
					XYZ item = Geometry.CalculatMidPoint(arc);
					list.Add(item);
					list.Add(hyendPoint2);
				}
				else
				{
					list.Add(hyendPoint);
					list.Add(hyendPoint2);
				}
				wire2 = HyRevitUtils.CreateWire(this.m_CurrentDocument, wireType2, this.m_CurrentDocument.ActiveView, wire.WiringType, list, null, null);
			}
			catch (Exception ex)
			{
				ex.ToString();
				transaction.RollBack();
			}
			transaction.Commit();
			if (wire2 == null)
			{
				return;
			}
			wire2.GroundConductorNum = wire.GroundConductorNum;
			wire2.HotConductorNum = wire.HotConductorNum;
			wire2.NeutralConductorNum = wire.NeutralConductorNum;
			ParameterCopier.CopyParameters(wire, ref wire2);
		}

		private Curve CreateCurveByCurveInfo(FindCurveInfo curveInfo)
		{
			XYZ startPoint = curveInfo.StartPoint;
			XYZ endPoint = curveInfo.EndPoint;
			Curve result;
			if (curveInfo.IsArc)
			{
				XYZ center = curveInfo.Center;
				double num = 0.0;
				Geometry.GetRadiusWithBulge(startPoint, endPoint, curveInfo.Bulge, ref num);
				XYZ xyz = Geometry.CalculatMidPoint(startPoint, endPoint);
				XYZ xyz2 = (xyz - center).Normalize() * num;
				xyz = center + xyz2;
				result = this.m_Revit.Application.ActiveUIDocument.Document.CreatYJKArc(startPoint, endPoint, xyz);
			}
			else
			{
				result = YJKLineEx.YJKGetBound(startPoint, endPoint);
			}
			return result;
		}

		private WireType FindWireType(Autodesk.Revit.DB.Document doc, string typeName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(WireType));
			if (filteredElementCollector != null)
			{
				foreach (Element element in filteredElementCollector.ToElements())
				{
					if (((WireType)element).Name == typeName)
					{
						return (WireType)element;
					}
				}
			}
			return null;
		}

		private ExternalCommandData m_Revit;

		private Document m_SourceDocument;

		private Document m_CurrentDocument;

		private double m_Tolerance;

		private Transform m_Transform;

		private bool m_IsMirrored;
	}
}
